package xyz.erupt.flow.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import xyz.erupt.core.annotation.EruptRouter;
import xyz.erupt.core.constant.EruptRestPath;
import xyz.erupt.core.view.EruptApiModel;
import xyz.erupt.flow.bean.entity.OaWorkFlowDefinition;
import xyz.erupt.flow.bean.entity.node.OaProcessNode;
import xyz.erupt.flow.bean.vo.WorkFlowHistoryTaskDetailVo;
import xyz.erupt.flow.bean.vo.WorkFlowHistoryTaskVo;
import xyz.erupt.flow.bean.vo.WorkFlowTaskDetailVo;
import xyz.erupt.flow.bean.vo.WorkFlowTaskVo;
import xyz.erupt.flow.constant.FlowConstant;
import xyz.erupt.flow.data.vo.VO_OaTaskHistory;
import xyz.erupt.flow.service.WorkFlowDefinitionService;
import xyz.erupt.flow.service.WorkFlowTaskService;
import xyz.erupt.upms.service.EruptUserService;

import java.util.List;

/**
 * @author PP
 * @version 1.0
 * @project flow
 * @description
 * @date 2024/11/4 11:42:34
 */
@RestController
@RequestMapping(EruptRestPath.ERUPT_API + "/" + FlowConstant.SERVER_NAME)
public class WorkFlowTaskController {
    @Autowired
    private EruptUserService eruptUserService;
    @Autowired
    private WorkFlowTaskService workFlowTaskService;
    @Autowired
    private WorkFlowDefinitionService workFlowDefinitionService;

    @PostMapping("/task/start/{id}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel startProcess(@PathVariable("id") Long id, @RequestBody JSONObject formContent){
        boolean res = workFlowTaskService.startFlow(id, formContent);
        if (res){
            return EruptApiModel.successApi();
        }else {
            return EruptApiModel.errorApi("流程已更新，请刷新流程");
        }

    }

    @GetMapping("/task/mine")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel getMyTask(String keywords,int pageSize,int pageIndex){
        String account = eruptUserService.getCurrentEruptUser().getAccount();
        Page<WorkFlowTaskVo> tasks = workFlowTaskService.getAllWorkFlowTaskByAccount(account, keywords, pageIndex, pageSize);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", tasks.getContent());
        jsonObject.put("total", tasks.getTotalElements());
        return EruptApiModel.successApi(jsonObject);

    }

    @GetMapping("/task/detail/{taskId}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel getTaskDetail(@PathVariable("taskId") String taskId){
        WorkFlowTaskDetailVo taskDetail = workFlowTaskService.getTaskDetailById(taskId);
        return EruptApiModel.successApi(taskDetail);

    }

    /**
     * 查询任务详情
     * @return
     */
    @GetMapping("/inst/detail/{instId}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel getInstDetail(@PathVariable("instId") String instId) {
        WorkFlowHistoryTaskDetailVo historyTaskDetail = workFlowTaskService.getHistoryTaskDetail(instId);
        return EruptApiModel.successApi(historyTaskDetail);
    }

    /**
     * 查询与我相关的流程
     * @return
     */
    @GetMapping("/inst/mine/about")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel getMineAbout(String keywords, int pageIndex, int pageSize) {
        String currentAccount = eruptUserService.getCurrentAccount();
        Page<WorkFlowHistoryTaskVo> allWorkFlowHistoryTaskByAccount = workFlowTaskService.getAllWorkFlowHistoryTaskByAccount(currentAccount, keywords, pageIndex, pageSize);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", allWorkFlowHistoryTaskByAccount.getContent());
        jsonObject.put("total", allWorkFlowHistoryTaskByAccount.getTotalElements());
        return EruptApiModel.successApi(jsonObject);
    }

    /**
     * 完成任务
     * @return
     */
    @PostMapping("/task/complete/{taskId}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel complete(@PathVariable("taskId") String taskId, @RequestBody JSONObject formContent) {
        String currentAccount = eruptUserService.getCurrentAccount();
        workFlowTaskService.complete(taskId,currentAccount,formContent);
//        taskService.complete(taskId, formContent.getString("remarks"), formContent.getJSONObject("data").toJSONString(),formContent.getString("sign"));
        return EruptApiModel.successApi();
    }

    /**
     * 拒绝任务
     * @return
     */
    @PostMapping("/task/refuse/{taskId}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel refuse(@PathVariable("taskId") String taskId, @RequestBody JSONObject formContent) {
        String currentAccount = eruptUserService.getCurrentAccount();
        Object o = formContent.get("remarks");
        String remarks="";
        if (o!=null){
            remarks = o.toString();
        }
        workFlowTaskService.refuse(taskId,currentAccount,remarks);
        return EruptApiModel.successApi();
    }


    /**
     * 预览流程时间线(查询bpmn流程图)
     * @return
     */
    @PostMapping("/process/timeline/preview/{processKey}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel preview(@PathVariable("processKey") String processKey) {
        String preview = workFlowDefinitionService.preview(processKey);
        return EruptApiModel.successApi(preview);
    }

    /**
     * 查看流程实例的时间线
     * @return
     */
    @PostMapping("/process/timeline/{taskId}")
    @EruptRouter(verifyType = EruptRouter.VerifyType.LOGIN)
    public EruptApiModel timeline(@PathVariable("taskId") String taskId) {
        List<VO_OaTaskHistory> voOaTaskHistories = workFlowTaskService.previewInstance(taskId);
        return EruptApiModel.successApi(voOaTaskHistories);
    }
}
